package editor;

import spark.utility.Clock;

import java.io.File;

import org.apache.log4j.Logger;
import blazingbubble.config.Config;
import blazingbubble.exception.StartupException;
import editor.graphics.Window;

public class LevelEditor {
	private static Logger logger = Logger.getLogger(LevelEditor.class);
	private static final String USAGE =
			"<resourcesPath>\n\n" +
			"-resourcesPath: path to the resources folder";
	private static final String NO_RESOURCES =
			"Resources folder does not exist";

	private static final int ARGS_INDEX_RESOURCES = 0;
	private static final int ARGS_COUNT = 1;
	private Window window;

	public void startup() throws StartupException {
		window = new Window();
	}

	public void execute() {
		window.setVisible(true);
		Clock clock = new Clock();
		clock.reset();
		while(window.isDisplayable()) {
			clock.update();
			window.update(clock.getElapsedSeconds());
		}
	}

	public static void main(String[] args) {
		if(args.length != ARGS_COUNT) {
			logger.fatal(USAGE);
			return;
		}
		File resorucesFolder = new File(args[ARGS_INDEX_RESOURCES]);
		if(!resorucesFolder.exists() || !resorucesFolder.isDirectory()) {
			logger.fatal(NO_RESOURCES + "(" + resorucesFolder.getAbsolutePath() + ")");
		}
		Config.setResourcePath(resorucesFolder.getPath());
		LevelEditor editor = new LevelEditor();
		try {
			editor.startup();
		}
		catch(StartupException e) {
			if(editor.window != null) {
				editor.window.dispose();
			}
			logger.fatal(e);
			return;
		}
		editor.execute();
	}
}
